//
//  PayPSWSelectViewController.m
//  PPDL
//
//  Created by 王洋 on 2019/7/24.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "PayPSWSelectViewController.h"

@interface PayPSWSelectViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *PayPswTF;
@property (weak, nonatomic) IBOutlet UITextField *surePayPswTF;

@end

@implementation PayPSWSelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
}

- (void)initView {
    
    [self.PayPswTF becomeFirstResponder];
    
    self.PayPswTF.delegate = self;
    self.surePayPswTF.delegate = self;
    
    [self.PayPswTF setSecureTextEntry:YES];
    [self.surePayPswTF setSecureTextEntry:YES];

    self.title = @"支付密码设置";
    
}

- (void)request_ChangePayPsw {
    
    NSDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.PayPswTF.text forKey:@"pwd"];
    [dic setValue:self.surePayPswTF.text forKey:@"pwd1"];

    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_MEMBER_CHANGE_PAY_PSW];
    
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        if ([data isKindOfClass:[NSDictionary class]]) {
            
            NSString * code = [data valueForKey:@"code"];
            if ([code isEqualToString:@"200"]) {
                
                [SVProgressHUD showSuccessWithStatus:[data valueForKey:@"msg"]];
                [SVProgressHUD dismissWithDelay:1];
                
                [self.navigationController popToRootViewControllerAnimated:YES];
                
            } else {
                
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"msg"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        } else {
            
        }
        
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
    }];
    
}

- (IBAction)requestChange:(id)sender {
    
    if (self.PayPswTF.text.length == 6 && self.PayPswTF.text.length == self.surePayPswTF.text.length) {
        
        //相等则请求
        if ([self.PayPswTF.text isEqualToString:self.surePayPswTF.text]) {
            
//            [self alert:@"确认修改"];
            [self request_ChangePayPsw];
            
        } else {
            
            [SVProgressHUD showInfoWithStatus:@"密码输入不一致"];
            [SVProgressHUD dismissWithDelay:1];
            
            [self.surePayPswTF becomeFirstResponder];
            
            [self.PayPswTF setSecureTextEntry:NO];
            [self.surePayPswTF setSecureTextEntry:NO];
        }
        
    } else {
        
        if (self.PayPswTF.text.length < 6) {
            
            [self.PayPswTF becomeFirstResponder];
        }
        
        if (self.surePayPswTF.text.length < 6) {
            
            [self.surePayPswTF becomeFirstResponder];
        }
        
        [SVProgressHUD showInfoWithStatus:@"您输入的密码位数没有6位"];
        [SVProgressHUD dismissWithDelay:1];
    }
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.PayPswTF) {
        // 这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            
            return YES;
            
        }  else if (self.PayPswTF.text.length >= 6) {
            
            self.PayPswTF.text = [textField.text substringToIndex:6];
            
            [SVProgressHUD showInfoWithStatus:@"支付密码为6位数字"];
            [SVProgressHUD dismissWithDelay:0.5];
            
            return NO;
        }
    }
    
    if (textField == self.surePayPswTF) {
        // 这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            
            return YES;
            
        }  else if (self.surePayPswTF.text.length >= 6) {
            
            self.surePayPswTF.text = [textField.text substringToIndex:6];
            
            [SVProgressHUD showInfoWithStatus:@"支付密码为6位数字"];
            [SVProgressHUD dismissWithDelay:0.5];
            
            return NO;
        }
    }
    
    return YES;
}


@end
